/********************************************************************************
 * DynamicTreeSelectAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Jan 5, 2009 6:55:59 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.util.CommonUtil;

public class DynamicTreeSelectAction extends ActionSupport {

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private String[] categories;

	private List<String> tmpCategories = new ArrayList<String>();

	private String nodeId;

	private String accept;

	private GeneralCategories currentCategory;

	private LocationCategories locationCategory;


    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }
    public String getNodeId() {
        return nodeId;
    }


    public String execute() throws Exception {
    	nodeId = CommonUtil.removeChar(nodeId, ',');

    	GeneralCategoriesWOS generalCategories = CategoryManagerServiceDelegator.getWOS(Integer.parseInt(nodeId));

    	if(generalCategories.getAccept() || accept != null)
    		categories = getGeneralCategoriesFromCategoryId(Integer.parseInt(nodeId));
    	else
    		categories = new String[]{"N/A"};

        return SUCCESS;
    }

    public String dynamicRegionTreeSelectAction() throws Exception {
    	nodeId = CommonUtil.removeChar(nodeId, ',');
    	locationCategory = getLocationCategoriesFromCategoryId(Integer.parseInt(nodeId));

        return SUCCESS;
    }

    public String[] getGeneralCategoriesFromCategoryId(int categoryid){
    	GeneralCategoriesWOS generalCategories = CategoryManagerServiceDelegator.getWOS(categoryid);
    	while (generalCategories.getCategoryId() != generalCategories.getGeneralCategories().getCategoryId()) {
    		tmpCategories.add(generalCategories.getCategoryName());
			generalCategories = generalCategories.getGeneralCategories();
		}
    	tmpCategories.add(generalCategories.getCategoryName());

    	Collections.reverse(tmpCategories);
    	return tmpCategories.toArray(new String[0]);
	}

    public LocationCategories getLocationCategoriesFromCategoryId(int categoryid){
    	LocationCategories locationCategories = LocationCategoriesServiceDelegator.get(categoryid);
    	while (locationCategories.getCategoryId() != locationCategories.getLocationCategories().getCategoryId()) {
    		tmpCategories.add(locationCategories.getCategoryName());
    		locationCategories = locationCategories.getLocationCategories();
		}
    	tmpCategories.add(locationCategories.getCategoryName());

    	Collections.reverse(tmpCategories);
    	categories = tmpCategories.toArray(new String[0]);

		return null;
	 }

    public String getNodeName() {
        return currentCategory != null ? currentCategory.getCategoryName() : "Node not found";
    }

	public String[] getCategories() {
		return categories;
	}
	public String getAccept() {
		return accept;
	}
	public void setAccept(String accept) {
		this.accept = accept;
	}
}
